<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../../common/jsp/taglibs.jsp"/>
<%
    String defaultSelectId = request.getParameter("defaultSelectId");
%>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>选择人员页面</title>
    <jsp:include page="../../common/jsp/head.jsp"/>
    <link type="text/css" href="${ctx}plugins/ztree/zTreeStyle/zTreeStyle.css?version=${version}" rel="stylesheet"/>
</head>
<body>
    <input type="hidden" id="defaultSelectId" value='<%=defaultSelectId%>' />
    <div class="content_wrap">
        <div class="zTreeDemoBackground left">
            <ul id="userTree" class="ztree"></ul>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    var userMap = new HashMap();
    //ztree设置
    var setting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "ps",
                "N": "ps"
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: zTreeOnClick
        }
    };


    var first = true;

    var form;
    var layer;
    //页面初始化时加载Layui框架
    layui.use(['form', 'layer'], function () {
        form = layui.form();
        layer = layui.layer;

        searchUser();
    });

    /**
     * 选择人员
     */
    function searchUser() {
        $.ajax({
            url: basePath + "user/selectUser.action",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var treeObj = $.fn.zTree.init($("#userTree"), setting, data);
                var defaultSelectId = $("#defaultSelectId").val();
                if (defaultSelectId == "" || defaultSelectId == null) {
                    // 默认展示第一个人员所在的组
                    var rootNodesArr = treeObj.getNodes();
                    for (var j = 0; j < rootNodesArr.length; j++) {
                        var nodes = treeObj.transformToArray(rootNodesArr[j]);
                        var isExpand = false;
                        for (var i = 0; i < nodes.length; i++) {
                            if (nodes[i].id.indexOf("uid_") == 0) {
                                var nodesArr = treeObj.getNodesByParam("id", nodes[i].id, null);
                                var tempNode = nodesArr[0];
                                while (tempNode.getParentNode() != null) {
                                    treeObj.expandNode(tempNode.getParentNode(), true);
                                    tempNode = tempNode.getParentNode();
                                }
                                isExpand = true;
                                break;
                            }
                        }
                        if (isExpand) {
                            break;
                        }
                    }
                }
                // 初始化后,同时初始化已经选择的人员
                if (first == true) {
                    initDefaultSelected();
                } else {
                    // 根据userMap初始化
                    initSelected();
                }
            }
        });
    }

    /**
     * 初始化已经选择的人员
     */
    function initDefaultSelected() {
        first = false;
        var defaultSelectId = $("#defaultSelectId").val();
        if ("" != defaultSelectId) {
            var ids = defaultSelectId.split(",");
            var treeObj = $.fn.zTree.getZTreeObj("userTree");
            for (var i = 0; i < ids.length; i++) {
                var node = treeObj.getNodesByParam("id", "uid_" + ids[i], null);
                for (var j in node) {
                    treeObj.checkNode(node[j], true, true);//选中默认勾选的节点
                    treeObj.selectNode(node[j], true, false);//节点数据/true多点选中(fasle单独选中)/false会让节点自动滚到到可视区域内(true不会)
                    treeObj.expandNode(node[j], true);
                    var treeNode = new TreeNode();
                    var id = node[j].id.substr(4);
                    treeNode.setId(id);
                    treeNode.setData(node[j].obj);
                    treeNode.setName(node[j].name);
                    treeNode.setType("user");
                    userMap.set(id, treeNode);
                }
            }
        }
    }

    function initSelected() {
        userMap.forEach(function (value, key) {
            var treeObj = $.fn.zTree.getZTreeObj("userTree");
            var node = treeObj.getNodesByParam("id", "uid_" + key, null);
            for (var j in node) {
                treeObj.checkNode(node[j], true, true);
                var treeNode = new TreeNode();
                var id = node[j].id.substr(4);
                treeNode.setId(id);
                treeNode.setData(node[j].obj);
                treeNode.setName(node[j].name);
                treeNode.setType("user");
                userMap.set(id, treeNode);
            }
        });
    }

    /**
     *节点被选中事件
     * @param event
     * @param treeId
     * @param treeNode
     */
    function zTreeOnClick(event, treeId, treeNode) {
        var treeObj = $.fn.zTree.getZTreeObj("userTree");
        var nodes = treeObj.getCheckedNodes(true); //获取选中的节点
        //循环开始
        for (var i = 0; i < nodes.length; i++) {
            var node = nodes[i];
            if (node.id.substr(0, 4) == "uid_") {
                //如果是人员，则保存到map里面
                var treeNode = new TreeNode();
                var id = node.id.substr(4);
                treeNode.setId(id);
                treeNode.setData(node.obj);
                treeNode.setName(node.name);
                treeNode.setType("user");
                userMap.set(id, treeNode);
            }
        }
        //循环结束
        art.dialog.data("userMap", userMap);
    }
</script>
<script type="text/javascript" src="${ctx}plugins/ztree/jquery.ztree.all-3.5.min.js?version=${version}"></script>